package com.example.okhttp3.testproject;

import com.example.okhttp3.utils.ILogUtil;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import android.support.test.runner.AndroidJUnit4;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 *
 * add by hujiangshui
 * mail 799423779@qq.com
 */
@RunWith(AndroidJUnit4.class)
public class OkHttp3UnitTest {
    public static final String TAG = OkHttp3UnitTest.class.getSimpleName();

    @Test
    public void addition_isCorrect() throws Exception {
        ILogUtil.d(TAG,"this is a test");
        assertEquals(4, 2 + 2);
    }

    @Test
    public void testOkhttpNormal() throws Exception  {
        ILogUtil.d(TAG,"test normal case");
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("180.163.150.161"); //this a www.huawei.com's ip,if you want find more huawei.com's ip could search the internet with DNS
        try {
            new OkHttp3Instance(500).doGetRequest("https://www.huawei.com", true, 0, addresses);
        } catch (IOException e) {
            ILogUtil.exception(TAG, e);
        }
    }

    @Test
    public void testConcurrentConnectWithSSLFailure(){
        ILogUtil.d(TAG,"test case ,the authentication failed");
        OkHttp3Instance http3Instance = new OkHttp3Instance(200);
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("103.232.215.139");   //authentication failed ,this souhu.com's ip
        addresses.add("14.215.177.39");  //this is baidu.com's ip, not authenticate ,will response error page

        try {
            http3Instance.doGetRequest("https://www.huawei.com", true, 0, addresses);
        } catch (IOException e) {
            ILogUtil.exception(TAG, e);
        }
    }

    @Test
    public void testConcurrentConnectNotSupportSSL(){
        ILogUtil.d(TAG,"test case not support ssh");
        OkHttp3Instance http3Instance = new OkHttp3Instance(200);
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("39.108.71.209");   //this is an aliyun cloud machine ,install nginx and start service
        addresses.add("60.28.100.36");  //huawei.com's athor ip, could find with internet
        try {
            http3Instance.doGetRequest("https://www.huawei.com", true, 0, addresses);
        } catch (IOException e) {
            ILogUtil.exception(TAG, e);
        }
    }


    @Test
    public void testConcurrentConnectNotOpenPort(){
        ILogUtil.d(TAG,"test case not open port");
        OkHttp3Instance http3Instance = new OkHttp3Instance(200);
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("39.108.71.209");   //when execute current test case ,you should first close all the port of http or https
        addresses.add("60.28.100.36");  //huawei.com's athor ip, could find with internet
        try {
            http3Instance.doGetRequest("https://www.huawei.com", true, 0, addresses);
        } catch (IOException e) {
            ILogUtil.exception(TAG, e);
        }
    }


    @Test
    public void testConcurrentConnectUnreachableAddress(){
        ILogUtil.d(TAG,"test case unreachable address");
        OkHttp3Instance http3Instance = new OkHttp3Instance(200);
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("132.34.56.76");   //public internet could not arrival address
        addresses.add("132.34.56.77");  //public internet could not arrival address
        try {
            http3Instance.doGetRequest("https://www.huawei.com", true, 0, addresses);
        } catch (IOException e) {
            ILogUtil.exception(TAG, e);
        }
    }
}